<?php
/**
 * Smarty {json} plugin
 *
 * Type:     function
 * Name:     json
 * Purpose:  fetch json file and  assign result as a template variable (array) || encode array to json date
 * @param url (url to fetch)
 * @param assign (element to assign to)
 * @return array|null if the assign parameter is passed, Smarty assigns the
 *                     result to a template variable
 */
function smarty_function_json($params, &$smarty)
{
    if (empty($params['data'])) {
        $smarty->_trigger_fatal_error("[json] parameter 'data' cannot be empty");
        return;
    }

    $data = $params['data'];

   /*
    if(!is_callable('json_decode')) {
    	require_once 'JSON.php';
	 	$json = new Services_JSON();
	 	$content = $json->decode( trim(file_get_contents($params['url'])) );
    } else {
    	$content = json_decode(trim(file_get_contents($params['url'])));
    }
    */
   if($params['exec'] == 'decode')
   {
      $data = json_decode($data);
   } else
   {
      $data = json_encode($data);
   }

    if (!empty($params['assign'])) {
        $smarty->assign($params['assign'],$data);
    } else {
        return $data;
    }
}
?>
